// map.js
import qqMap from '../../../utils/qqmap'
Page({
  data: {
    latitude: ' 28.194178',
    longitude: '112.98298',
    repairDetail:{},
    markers: [
      {
        id: 1,
        latitude: 28.194178,
        longitude: 112.98298,
        width: 24,
        height: 30,
      },
      {
        id: 2,
        latitude: 27.882942,
        longitude: 112.863096,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40,
      },
    ],
  },
  onLoad({id}){
    this.getRepairDetail(id),
    this.getPolyline()
  },
  async getRepairDetail(id){
    const {code,data}=await wx.http.get(`/repair/${id}`)
    if (code !== 10000) return wx.utils.toast()
    // console.log(data);
    this.setData({ repairDetail:data })
  },
  getPolyline(){
    qqMap.direction({
      mode: 'bicycling',//可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
      //from参数不填默认当前地址
      from: '28.194178,112.98298',
      to:  '27.882942,112.863096', 
      success: (res)=> {
        console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        this.setData({
          latitude:pl[0].latitude,
          longitude:pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      }
    });
  },
  async cancelRepair(ev){
    const {code}=await wx.http.put('/cancel/repaire/' + ev.mark.id)
    if (code !== 10000) return wx.utils.toast()
    // 跳转到报修列表页面
    wx.navigateTo({
      url: '/repair_pkg/pages/list/index',
    })
  }
})
